import pandas as pd
import time
from datetime import datetime
import os
import numpy as np
import json 
import sys


work_dir = os.path.abspath(sys.argv[0])
file_path = '\\'.join(work_dir.split('\\')[:-1])
# os.chdir(file_path)

# print(sys.path)
# print(os.getcwd())

setting_json = ""
with open('..\\conf\\config.json', 'r') as f:
    for line in f.read():
        setting_json += line

setting_dict = json.loads(setting_json)
save_path = setting_dict['db']

def generate_time():
    hour_list = list(range(24))
    minute_list = ['0'+str(i) if i<10 else i for i in list(range(60))]

    hour_minute_list = []

    for h in hour_list:
        for m in minute_list:
            hour_minute = "{}:{}".format(h, m)
            hour_minute_list.append(hour_minute)
    return hour_minute_list


def generate_time_sheet(hour_minute_list):
    """
    #! 如果文件已存在则不再创建，也不覆盖
    """
    if os.path.isfile(save_path):
        pass
    else:
        df = pd.DataFrame(index=hour_minute_list)
        # df.index = hour_minute_list
        df.to_csv(save_path, encoding='utf_8_sig')


def record_date():
    now = datetime.now()
    date = "{}月{}日".format(now.month, now.day)
    return date


def recorder():
    table = pd.read_csv(save_path, encoding='utf_8_sig', index_col=0)
    # table.rename(index={"Unnamed: 0":"time"}, inplace=True)
    event = str(input('输入事件:'))
    start_time = str(input('输入开始时间(hh:mm):'))
    end_time = str(input('输入结束时间(hh:mm):'))
    #! 要先判断是否到了新的一天，为新的一天创建一列
    date = record_date()
    if date in table.columns:
        table.loc[start_time:end_time, date] = event
    else:
        table[date] = np.nan
        table.loc[start_time:end_time, date] = event
    
    table.to_csv(save_path, encoding='utf_8_sig')

def recorder2(event, start_time, end_time):
    table = pd.read_csv(save_path, encoding='utf_8_sig', index_col=0)
    # table.rename(index={"Unnamed: 0":"time"}, inplace=True)

    #! 要先判断是否到了新的一天，为新的一天创建一列
    date = record_date()
    if date in table.columns:
        table.loc[start_time:end_time, date] = event
    else:
        table[date] = np.nan
        table.loc[start_time:end_time, date] = event
    
    table.to_csv(save_path, encoding='utf_8_sig')
    
def run():
    time_list = generate_time()
    generate_time_sheet(time_list)
    recorder()

def run2(event, start_time, end_time):
    time_list = generate_time()
    generate_time_sheet(time_list)
    recorder2(event, start_time, end_time)

if __name__ =="__main__":
    run()